package com.sxz.sbs.module.client;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.constant.IntentConstant;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.CarListAdapter;
import com.sxz.sbs.module.client.bean.CarListBean;
import com.sxz.sbs.module.client.presenter.CarListPresenter;
import com.sxz.sbs.module.client.view.CarListView;
import com.sxz.sbs.widget.CommonTitle;
import com.sxz.sbs.widget.StallAlertDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hp
 * @date 2018/1/11
 * 车辆列表界面
 */

public class CarListActivity extends BaseActivity<CarListPresenter> implements CarListView {

    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.iv_title_standby)
    ImageView imgTitleStandby;

    private List<CarListBean> list = new ArrayList<>();
    private CarListAdapter carListAdapter;
    private int customId;
    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("车辆列表").showRightReadyImg(R.drawable.ic_adds).build().show();
        customId = getIntent().getIntExtra(IntentConstant.CUSTOM_ID,0);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setRefreshing(true);
        carListAdapter = new CarListAdapter(list);
        recyclerView.setAdapter(carListAdapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getCarListData();
            }
        });
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                getCarListData();
            }
        });

        carListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
                new StallAlertDialog.Builder(CarListActivity.this)
                        .setMessage("确定要删除该车辆吗")
                        .setPositiveButton(new StallAlertDialog.OnPositiveListener() {
                            @Override
                            public void onPositiveClick(View view) {
                                HashMap<String,Object> map = HttpParamMap.getParamMap();
                                map.put("carId",list.get(position).getId());
                                mPresenter.deleteCarList(map);
                            }
                        }).create().show();

            }
        });
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(CarListActivity.this,AddCarActivity.class)
                        .putExtra("car_bean",list.get(position)));
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        getCarListData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_carlist;
    }

    @Override
    protected CarListPresenter createPresenter() {
        return new CarListPresenter();
    }


    @OnClick({R.id.iv_title_standby})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_standby:
                Intent intent=new Intent(CarListActivity.this,AddCarActivity.class);
                intent.putExtra(IntentConstant.CUSTOM_ID,customId);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
        carListAdapter.loadMoreFail();
    }

    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (errorTag == AppConstant.NET_ERROR_CODE) {
            stateLayout.showNetwork();
        } else {
            stateLayout.showEmpty();
        }

    }
    @Override
    public void onCarListSuccess(List<CarListBean> data) {
        if (mIsLive && data != null) {
            refreshLayout.setRefreshing(false);
            if (data.size() > 0) {
                stateLayout.showContent();
                list.clear();
                list.addAll(data);
                carListAdapter.notifyDataSetChanged();
                carListAdapter.loadMoreComplete();
            } else {
                list.clear();
                carListAdapter.notifyDataSetChanged();
                //  数据为空
                stateLayout.showEmpty();
            }
        }
    }

    @Override
    public void onDeleteCarSuccess() {
        getCarListData();
    }

    private void getCarListData() {
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("customId", customId);
        mPresenter.getCarList(map);
    }
}
